//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.wsd.smartcarwasher.service;

import com.wsd.smartcarwasher.dao.CardDao;
import com.wsd.smartcarwasher.dao.CardOrderDao;
import com.wsd.smartcarwasher.domain.Card;
import com.wsd.smartcarwasher.domain.CardOrder;
import com.wsd.smartcarwasher.domain.Pagination;
import com.wsd.smartcarwasher.domain.UserCard;
import com.wsd.smartcarwasher.util.DateUtil;
import com.wsd.smartcarwasher.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class CardOrderService {
    private static final Logger logger = LoggerFactory.getLogger(CardOrderService.class);
    @Resource
    private CardOrderDao cardOrderDao;

    @Resource
    private CardDao cardDao;
    @Autowired(required = false)
    private CardService cardService;


    public CardOrderService() {
    }
    public int addCardOrder(CardOrder cardOrder) {
        cardOrder.setOrderSn(this.generateOrderSn());
        cardOrder.setCreateTime(DateUtil.getSysTime());
        cardOrder.setOrderStatus(100);
        cardOrder.setPayStatus(1);
        return this.cardOrderDao.addCardOrder(cardOrder);
    }

    public int updateCardOrder(CardOrder cardOrder) {
        return this.cardOrderDao.updateCardOrder(cardOrder);
    }
    public int deleteCardOrder(String orderSn) {
        return this.cardOrderDao.deleteCardOrder(orderSn);
    }
    public CardOrder queryOrderInfoByOrderSn(String orderSn) {
        return this.cardOrderDao.queryOrderInfoByOrderSn(orderSn);
    }

    public Map<String,Object> queryOrderandCardInfoByOrderSn(String orderSn)
    {
        CardOrder cardOrder=this.cardOrderDao.queryOrderInfoByOrderSn(orderSn);
        Card card=this.cardDao.queryCardByCardId(cardOrder.getCardId());
        Map<String,Object> map=new HashMap<>();
        map.put("card",card);
        map.put("cardOrder",cardOrder);
        return map;
    }


    private List<String> getAllOrderSn() {
        List<String> orderSns = this.cardOrderDao.getAllOrderSn();
        if (orderSns == null) {
            orderSns = Collections.emptyList();
        }
        return orderSns;
    }

    private String generateOrderSn() {
        String orderSn = StringUtil.getOrderSn();

        for(List orderSns = this.getAllOrderSn(); orderSns.contains(orderSn); orderSn = StringUtil.getOrderSn()) {
        }

        return orderSn;
    }

    public List<CardOrder> queryUserOrder(String userId) {
        List<CardOrder> orders = this.cardOrderDao.queryUserOrder(userId);
        if (orders == null) {
            orders = Collections.emptyList();
        }
        return orders;
    }

    public void payFinishOrder(CardOrder order) {
        order.setPayStatus(2);
        order.setPayTime(new Date());
        int changeRow = this.updateCardOrder(order);
        if (changeRow == 0) {
            logger.error("修改会员卡订单支付状态失败, orderSn=" + order.getOrderSn() + ",payTime=" + order.getPayTime());
        }
        Integer cardId = order.getCardId();
        String userId = order.getUserId();
        if (!this.cardService.bindCardToUser(userId, cardId,order.getUcId())) {
            logger.error("绑定会员卡到用户失败, userId=" + userId + ",cardId=" + cardId);
        }
    }

    public void payHeFinishOrder(CardOrder order,String heUserId) {
        order.setPayStatus(2);
        order.setPayTime(new Date());
        order.setIsHepaid(1);
        order.setHeuserId(heUserId);
        int changeRow = this.updateCardOrder(order);
        if (changeRow == 0) {
            logger.error("修改会员卡订单支付状态失败, orderSn=" + order.getOrderSn() + ",payTime=" + order.getPayTime());
        }
        Integer cardId = order.getCardId();
        String userId = order.getUserId();
        if (!this.cardService.bindCardToUser(userId, cardId,order.getUcId())) {
            logger.error("绑定会员卡到用户失败, userId=" + userId + ",cardId=" + cardId);
        }

    }
    public Pagination<CardOrder> queryUserOrderAll(int Page,int PageSize,CardOrder cardOrder) {
        List<CardOrder> ls = this.cardOrderDao.queryUserOrderAll(cardOrder);

        ls.forEach(cardOrder1 -> {
            cardOrder1.setCard(cardService.queryCardByCardId(cardOrder1.getCardId()));
            cardOrder1.setUserCardList(cardDao.queryUserCardByUcId(cardOrder1.getUcId()));
        });

        return new Pagination<CardOrder>(Page,PageSize,ls);

    }
    /**
     * 根据会员卡号查询订单
     * @param ucId
     * @return
     */
    public CardOrder queryOrderInfoByUcId(String ucId) {
        return this.cardOrderDao.queryOrderInfoByUcId(ucId);
    }
}


/*
package com.wsd.smartcarwasher.service;

import com.wsd.smartcarwasher.constans.Global;
import com.wsd.smartcarwasher.dao.CardOrderDao;
import com.wsd.smartcarwasher.domain.Card;
import com.wsd.smartcarwasher.domain.CardOrder;
import com.wsd.smartcarwasher.util.DateUtil;
import com.wsd.smartcarwasher.util.StringUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class CardOrderService {

    @Resource
    private CardOrderDao cardOrderDao;

    @Resource
    private CardService cardService;

    public int addCardOrder(CardOrder cardOrder) {
        cardOrder.setOrderSn(generateOrderSn());
        cardOrder.setCreateTime(DateUtil.getSysTime());
        cardOrder.setOrderStatus(Global.ORDER_STATUS_NORMAL);
        cardOrder.setPayStatus(Global.ORDER_PAY_STATUS_PAYING);
        return cardOrderDao.addCardOrder(cardOrder);
    }

    public int updateCardOrder(CardOrder cardOrder) {
        return cardOrderDao.updateCardOrder(cardOrder);
    }

    public int deleteCardOrder(String orderSn){
        return cardOrderDao.deleteCardOrder(orderSn);
    }

    public CardOrder queryOrderInfoByOrderSn(String orderSn) {
        return cardOrderDao.queryOrderInfoByOrderSn(orderSn);
    }

    private List<String> getAllOrderSn() {
        List<String> orderSns = cardOrderDao.getAllOrderSn();
        if (orderSns == null) {
            orderSns = Collections.emptyList();
        }
        return orderSns;
    }

    private String generateOrderSn() {
        String orderSn = StringUtil.getOrderSn();
        List<String> orderSns = getAllOrderSn();
        while (orderSns.contains(orderSn)) {
            orderSn = StringUtil.getOrderSn();
        }
        return orderSn;
    }

    public List<CardOrder> queryUserOrder(String userId) {
        List<CardOrder> orders = cardOrderDao.queryUserOrder(userId);
        if (orders == null) {
            orders = Collections.emptyList();
        }
        List<Card> cards = cardService.queryAllCard();
        for (CardOrder order: orders) {
            Optional<Card> card = cards.parallelStream().filter(c -> order.getCardId().equals(c.getId())).findFirst();
            if (card.isPresent()) {
                order.setCard(card.get());
            }
        }
        return orders;
    }
}
*/
